package itman.controller;

import itman.feign.FoodFeignClient;
import itman.po.Business;
import itman.po.CommonResult;
import itman.service.BusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@CrossOrigin("*")
@RestController
@RequestMapping("/BusinessController")
public class BusinessController {

    @Autowired
    private BusinessService businessService;
    @Resource
    private FoodFeignClient foodFeignClient;

    @GetMapping("/listBusinessByOrderTypeId/{orderTypeId}")
    public CommonResult<List> listBusinessByOrderTypeId(@PathVariable("orderTypeId") Integer orderTypeId) throws Exception{
        List<Business> list = businessService.listBusinessByOrderTypeId(orderTypeId);
        return new CommonResult(200,"success",list);
    }

    @GetMapping("/getBusinessById/{businessId}")
    public CommonResult<Business> getBusinessById(@PathVariable("businessId") Integer businessId) throws Exception{


        Business business = businessService.getBusinessById(businessId);
        //使用DiscoveryClient获取元数据，主机地址与端口就可以不硬编码了
        CommonResult<List> result = foodFeignClient.listFoodByBusinessId(businessId);
        System.out.println(result.getMessage());
        if(result.getCode()==200) {
            business.setFoodList(result.getResult());
        }else {
            business.setFoodList(new ArrayList<>());
        }
        return new CommonResult(200,"success",business);
    }
}
